<?php

class ectools_ctl_admin_messenger_config extends desktop_controller {

    public function __construct($app) {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    /**
     * 短信接口设置
     * @author Wangfy
     * @date 2019/06/19
     */
    function index() {
        $actions_base['title'] = app::get('ectools')->_('短信平台设置');
        $actions_base['use_buildin_recycle'] = false;
        
        $this->finder('ectools_mdl_messenger_config', $actions_base);
    }

    /**
     * 显示配置信息页面
     * @author Wangfy
     * @date 2019年7月19日
     * @param string $appClass 短信平台类名即配置缓存名
     */
    function showSetInfo($appClass, $appKey) {
        if (! $appClass) {
            $this->begin();
            $this->end(false, '参数异常');
        }
        $sms = new $appClass($this->app);
        $setting = $sms->setting();
        if ($setting) {
            $val = $this->app->getConf($appClass);
            $val = unserialize($val);
            $render = $this->app->render();
            $render->pagedata['sms_brief'] = $sms->getSmsbrief();
            $render->pagedata['settings'] = $setting;
            foreach ($setting as $k => $v) {
                $render->pagedata['settings'][$k]['value'] = $val['setting'][$k] ? $val['setting'][$k] : $val[$k];
            }
            $render->pagedata['classname'] = $appClass;
            $render->pagedata['smskey'] = $appKey;
            $render->display('messenger/config.html');
        } else {
            echo '<div class="note">' . app::get("ectools")->_("不需要设置参数") . '</div>';
        }
    }

    /**
     * 保存配置信息
     * @author Wangfy
     * @date 2019年7月19日
     * @param string $appClass 短信平台类名即配置缓存名
     */
    function setting($appClass, $appKey) {
        $this->begin('javascript:finderGroup["' . $_POST['finder_id'] . '"].refresh();');
        $sms = new $appClass($this->app);
        $setting = $sms->setting();
        $data['setting'] = $_POST['setting'];
        
        $this->app->setConf($appClass, serialize($data));
        $this->end(true, app::get('ectools')->_('短信平台修改成功！'));
    }

    /**
     * 显示通道使用设置页面
     * @author Wangfy
     * @date 2019年7月22日
     */
    function showUseSetting() {
        // 获取当前平台配置开启的短信平台
        $smsMdl = $this->app->model('messenger_config');
        $smsList = $smsMdl->getSmsList('sms_name,app_key', array(
            'sms_status' => 'true'
        ));
        // 获取当前平台短信类型
        $smsService = kernel::single('ectools_messenger_app');
        $arrSmsType = $smsService->arrSmsType;
        // 获取当前平台短信类型已设置使用的通道
        $smsUseStr = $this->app->getConf('sms.function.use');
        $smsUseArr = unserialize($smsUseStr);
        
        $render = $this->app->render();
        $render->pagedata['smsList'] = $smsList;
        $render->pagedata['smsType'] = $arrSmsType;
        $render->pagedata['smsUse'] = $smsUseArr;
        $render->display('messenger/useSetting.html');
    }

    /**
     * 保存通道使用设置
     * @author Wangfy
     * @date 2019年7月22日
     */
    function saveUseSetting() {
        $this->begin();
        $this->app->setConf('sms.function.use', serialize($_POST));
        $this->end(true, app::get('ectools')->_('保存成功'));
    }
}
?>
